using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace JelleDruyts.Mollom.Client
{
    /// <summary>
    /// Represents an XML-RPC fault exception.
    /// </summary>
    [Serializable]
    public class XmlRpcFaultException : XmlRpcException
    {
        #region Properties

        private readonly int faultCode;

        /// <summary>
        /// Gets the fault code.
        /// </summary>
        public int FaultCode
        {
            get { return this.faultCode; }
        }

        private readonly string faultString;

        /// <summary>
        /// Gets the fault string.
        /// </summary>
        public string FaultString
        {
            get { return this.faultString; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcFaultException"/> class.
        /// </summary>
        public XmlRpcFaultException()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcFaultException"/> class.
        /// </summary>
        /// <param name="message">The exception message.</param>
        public XmlRpcFaultException(string message)
            : base(message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcFaultException"/> class.
        /// </summary>
        /// <param name="message">The exception message.</param>
        /// <param name="innerException">The inner exception.</param>
        public XmlRpcFaultException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcFaultException"/> class.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"></see> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"></see> is zero (0).</exception>
        /// <exception cref="T:System.ArgumentNullException">The info parameter is null.</exception>
        protected XmlRpcFaultException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            if (info != null)
            {
                this.faultString = info.GetString("faultString");
                this.faultCode = info.GetInt32("faultCode");
            }
        }

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"></see> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.ArgumentNullException">The info parameter is a null reference (Nothing in Visual Basic).</exception>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/></PermissionSet>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            if (info != null)
            {
                info.AddValue("faultString", this.faultString);
                info.AddValue("faultCode", this.faultCode);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcFaultException"/> class.
        /// </summary>
        /// <param name="message">The exception message.</param>
        /// <param name="faultString">The fault string.</param>
        /// <param name="faultCode">The fault code.</param>
        public XmlRpcFaultException(string message, string faultString, int faultCode)
            : base(message)
        {
            this.faultCode = faultCode;
            this.faultString = faultString;
        }

        #endregion
    }
}